use "data\out\estsample.dta", clear 


* forbruksgrupper - lav til høy 
gegen pre_forbruk = mean(exp(lnforbruk)) if month == 12 & day <= 7, by(maalepktnr)
gegen int pre_forbruk_ = cut(pre_forbruk) if pre_forbruk != ., group(4)
gegen int pre_forbruk_q = max(pre_forbruk_), by(maalepktnr)
drop pre_forbruk pre_forbruk_

fmerge m:1 maalepktnr using "data\in\maalepktnr_grunnkretsincome.dta", keep(match) nogenerate

g byte PeakxEvent = Peak*EventDay
g byte ShoulderxEvent = shoulder*EventDay

* per (grunnkrets)inntektskvartil 
forval i = 0(2)2 {
ivreghdfe lnforbruk c.temp##c.temp##c.temp temp_* Peak PeakxEvent ShoulderxEvent Peakelcar shoulder shoulderelcar ///
  (Effektpris EffektprisNonPeak Effektprisnext EffektprisNonPeaknext Effektpriselcar EffektprisNonPeakelcar Effektprisnextelcar EffektprisNonPeaknextelcar Effektprisshoulder Effektprisshoulderelcar =  TPE TNPE TPnextdays TNPnextdays TPEelcar TNPEelcar TPnextdayselcar TNPnextdayselcar TsE TsEelcar) if income_q == `i' | income_q == `i' + 1, absorb(maalepktnr dato) cluster(maalepktnr)
est store inc`i'  
estadd ysumm  
gunique maalepktnr if e(sample) & elcar == 1 
estadd scalar elcars = r(unique): inc`i'  
}

* consumption groups 
forval i = 0(2)2 {
ivreghdfe lnforbruk c.temp##c.temp##c.temp temp_* Peak PeakxEvent ShoulderxEvent Peakelcar shoulder shoulderelcar ///
  (Effektpris EffektprisNonPeak Effektprisnext EffektprisNonPeaknext Effektpriselcar EffektprisNonPeakelcar Effektprisnextelcar EffektprisNonPeaknextelcar Effektprisshoulder Effektprisshoulderelcar =  TPE TNPE TPnextdays TNPnextdays TPEelcar TNPEelcar TPnextdayselcar TNPnextdayselcar TsE TsEelcar) if pre_forbruk_q == `i' | pre_forbruk_q == `i' + 1, absorb(maalepktnr dato) cluster(maalepktnr)
est store c`i'
estadd ysumm  
gunique maalepktnr if e(sample) & elcar == 1 
estadd scalar elcars = r(unique): c`i' 
}

* wattmeter
ivreghdfe lnforbruk c.temp##c.temp##c.temp temp_* Peak PeakxEvent ShoulderxEvent Peakelcar shoulder shoulderelcar ///
  (Effektpris EffektprisNonPeak Effektprisnext EffektprisNonPeaknext Effektpriselcar EffektprisNonPeakelcar Effektprisnextelcar EffektprisNonPeaknextelcar Effektprisshoulder Effektprisshoulderelcar =  TPE TNPE TPnextdays TNPnextdays TPEelcar TNPEelcar TPnextdayselcar TNPnextdayselcar TsE TsEelcar) if inlist(stratgroups,2,5,8,11) == 1, absorb(maalepktnr dato) cluster(maalepktnr)
est store watt 
estadd ysumm  
gunique maalepktnr if e(sample) & elcar == 1 
estadd scalar elcars = r(unique): watt 

* no wattmeter
ivreghdfe lnforbruk c.temp##c.temp##c.temp temp_* Peak PeakxEvent ShoulderxEvent Peakelcar shoulder shoulderelcar ///
  (Effektpris EffektprisNonPeak Effektprisnext EffektprisNonPeaknext Effektpriselcar EffektprisNonPeakelcar Effektprisnextelcar EffektprisNonPeaknextelcar Effektprisshoulder Effektprisshoulderelcar =  TPE TNPE TPnextdays TNPnextdays TPEelcar TNPEelcar TPnextdayselcar TNPnextdayselcar TsE TsEelcar) if inlist(stratgroups,2,5,8,11) != 1, absorb(maalepktnr dato) cluster(maalepktnr)
est store nowatt 
estadd ysumm  
gunique maalepktnr if e(sample) & elcar == 1 
estadd scalar elcars = r(unique): nowatt  

estout inc0 inc2 watt nowatt c0 c2 using "output\table_3_subgroups.txt", replace type ///
  cells(b( fmt(3)) se(par fmt(3)) _star) indicate(temp) mlabels() collabels(none) legend ///
  order(Effektpris EffektprisNonPeak Effektprisnext EffektprisNonPeaknext Peak PeakxEvent ShoulderxEvent Effektpriselcar EffektprisNonPeakelcar Effektprisnextelcar EffektprisNonPeaknextelcar Peakelcar Effektprisshoulder shoulder Effektprisshoulderelcar shoulderelcar) ///
  varlabels(TPE TP TNPE TNP) starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(26) ///
  stats(ymean r2 N_clust elcars N, fmt(3 3 0 0 0)) style(tex)


